﻿using FirstView.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PublishingHouse.Infrastructure
{
    public class CustomerRepository
    {
        public void Add(string Registerid, string password, string phone)
        {
            using (pressmannersystemContext context = new pressmannersystemContext())
            {
                Customers customer = new Customers
                {
                    Cid = Registerid,
                    Name = "亲",
                    Sex = "男",
                    Age = 18,
                    Address = "默认地址",
                    Password = password,
                    Phone = phone
                };
                context.Customers.Add(customer);
                context.SaveChanges();
            }
        }
        public void AddOrder(Order ordermodle)
        {
            using (pressmannersystemContext context = new pressmannersystemContext())
            {
                Order order = new Order
                {
                    Cid = ordermodle.Cid,
                    OrderStatus = ordermodle.OrderStatus,
                    OrderTime = ordermodle.OrderTime,
                    TotalPrice = ordermodle.TotalPrice,
                    Bookname = ordermodle.Bookname,
                    Bookprice = ordermodle.Bookprice,
                    Quantity = ordermodle.Quantity,
                    Address = ordermodle.Address
                };
                context.Order.Add(order);
                context.SaveChanges();
            }
        }
        public void ModifyPassword(string id,string newpassword)
        {
            using (var context = new pressmannersystemContext())
            {
                var customer = Query(id);
                customer.Password = newpassword;
                context.Customers.Update(customer);
                context.SaveChanges();
            }
                
        }
        public void Delete(string id)
        {
            using (var context = new pressmannersystemContext())
            {
                var customer = context.Customers.FirstOrDefault(x => x.Cid == id);
                context.Customers.Remove(customer);
                context.SaveChanges();
            }
        }
        public void Update(Customers customer)
        {
            using (var context = new pressmannersystemContext())
            {
                context.Customers.Update(customer);
                context.SaveChanges();
            }
        }
        public void UpdateBook(Book book)
        {
            using (var context = new pressmannersystemContext())
            {
                context.Book.Update(book);
                context.SaveChanges();
            }
        }
        public void UpdateOrder(Order order)
        {
            using (var context = new pressmannersystemContext())
            {
                context.Order.Update(order);
                context.SaveChanges();
            }
        }
        public Customers Query(string id)
        {
            Customers customer = null;
            using (var context = new pressmannersystemContext())
            {
                customer = context.Customers.FirstOrDefault(x => x.Cid == id);
                context.SaveChanges();
            }
            return customer;
        }
        public Book QueryBook(string isbn)
        {
            Book book = null;
            using (var context = new pressmannersystemContext())
            {
                book = context.Book.FirstOrDefault(x => x.Isbn == isbn);
                context.SaveChanges();
            }
            return book;
        }
        public Author QueryAuthor(string id)
        {
            Author author = null;
            using (var context = new pressmannersystemContext())
            {
                author = context.Author.FirstOrDefault(x => x.Id == id);
                context.SaveChanges();
            }
            return author;
        }
        public List<Bookauthor> QueryBookAuthor()
        {
            List<Bookauthor> bookauthorlist = new List<Bookauthor>();
            using (var context = new pressmannersystemContext())
            {
                bookauthorlist = (from u in context.Bookauthor
                                  select u
                            ).ToList();
                context.SaveChanges();
            }
            return bookauthorlist;
        }
        public List<Order> QueryOrder(string cid)
        {
            List<Order> listOrder = new List<Order>();
            using (var context = new pressmannersystemContext())
            {
                listOrder = (from u in context.Order
                             where u.Cid == cid
                            select u
                             ).ToList();
                context.SaveChanges();
            }
            return listOrder;
        }
        public Order QueryReturnOrder(int id)
        {
            Order order = null;
            using (var context = new pressmannersystemContext())
            {
                order = context.Order.FirstOrDefault(x => x.Id == id);
                context.SaveChanges();
            }
            return order;
        }
    }
}
